import { isObject, isDate, getStartDate } from "./utils";

export default {
  methods: {
    setGridWidth() {
      const width = this.gantt.config.grid_width;
      if (width > 0) {
        this.gantt.config.grid_width = 0;
      } else {
        this.gantt.config.grid_width = this.defaultLeftWidth;
      }
      this.gantt.render();
      return this.gantt.config.grid_width;
    },
    move(date) {
      const { data } = this.value;
      if (data.length === 0) {
        throw new Error("请在有数据的情况下操作");
      }
      const { left, top } = this.gantt.getTaskPosition(
        { id: data[0].id },
        new Date(date)
      );
      this.gantt.scrollTo(left - 100, top);
    },
    deleteTask({ id }) {
      this.gantt.deleteTask(id);
    },
    addLink(data) {
      this.gantt.addLink(data);
    },
    deleteLink({ id }) {
      console.log("asdasdazz", id);
      this.gantt.deleteLink(id);
    },
    updateLink(source) {
      source.forEach((v) => {
        const origin = this.gantt.getLink(v.id);
        for (const key in source) {
          origin[key] = source[key];
        }
        this.gantt.updateTask(v.id);
      });
    },
    updateTaskData(source) {
      if (isObject(source) && Object.keys(source).length > 0) {
        const id = source.id;
        const task = this.gantt.getTask(id);
        for (const key in source) {
          if (key === "start_date" || key === "end_date") {
            source[key] = isDate(source[key])
              ? source[key]
              : new Date(source[key]);
          } else if (key === "duration") {
            const startDate = getStartDate(source) || task.start_date;
            if (startDate) {
              const endTime = this.gantt.calculateEndDate({
                start_date: startDate,
                duration: source[key],
                task: task
              });
              task.end_date = endTime;
            }
          }
          task[key] = source[key];
        }
        this.gantt.updateTask(id);
      } else {
        source.forEach((v) => {
          this.gantt.updateTask(v.id, v);
        });
      }
    },

    compare(soure, target) {
      if (soure.length > target.length) {
        const data = soure.find((v) => !target.map((s) => s.id).includes(v.id));
        return {
          type: "add",
          data
        };
      } else if (soure.length < target.length) {
        const data = target.find((v) => !soure.map((s) => s.id).includes(v.id));
        return {
          type: "del",
          data
        };
      }
      return {
        type: "update",
        data: soure
      };
    },
    update(task, type = "data") {
      const sourceData = task[type];
      const mapActionType = {
        data: {
          target: this.gantt.getDatastore("task")._eachItemMainRangeCache,
          add: (data) => this.gantt.addTask(data),
          del: (data) => this.deleteTask(data),
          update: (data) => this.updateTaskData(data)
        },
        links: {
          target: this.gantt.getLinks(),
          add: (data) => this.gantt.addLink(data),
          del: (data) => this.deleteLink(data),
          update: (data) => this.updateLink(data)
        }
      };
      const target = mapActionType[type].target;
      const actionType = this.compare(sourceData, target);
      mapActionType[type][actionType.type](actionType.data);
    },
    addTask(data) {
      this.gantt.addTask(data);
    },
    updateTask(task) {
      this.update(task, "data");
    },
    updateLine(task) {
      this.update(task, "links");
    },
    getSelectTask() {
      const result = [];
      const targetId = this.gantt.getSelectedTasks();
      if (targetId.length > 0) {
        targetId.forEach((v) => {
          const task = this.gantt.getTask(v);
          result.push(task);
        });
      }
      return result;
    },
    setIgnoreDate(target, status = false) {
      console.log(target, "target");
      this.gantt.ignore_time = (date) => {
        console.log(date.getDay(), target);
        if (target.includes(date.getDay())) {
          return status;
        }
        // if (date.getDay() === 0 || date.getDay() === 6) { return status }
      };
      // this.gantt.render()
    }
  }
};
